﻿using Factory.Common;
using Factory.MasterData.MaterialClassifications.Dto;
using Factory.MasterData.Materials;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Entities;
using Volo.Abp.Domain.Repositories;

namespace Factory.MasterData.MaterialClassifications
{
    /// <summary>
    /// 物料产品分类服务
    /// </summary>
    public class MaterialClassificationAppService : ApplicationService, IMaterialClassificationAppService
    {
        /// <summary>
        /// 物料产品仓储
        /// </summary>
        private readonly IRepository<MaterialClassification, int> _materialrepository;

        private readonly IRepository<MaterialClassification> repository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="materialrepository">物料产品仓储</param>
        public MaterialClassificationAppService(IRepository<MaterialClassification, int> materialrepository, IRepository<MaterialClassification> repository)
        {
            _materialrepository = materialrepository;
            this.repository = repository;
        }


        /// <summary>
        /// 查询物料产品分类树形图
        /// </summary>
        /// <param name="product">是否是产品/物料</param>
        /// <param name="sysName">名称</param>
        /// <returns>返回结果</returns>
        public async Task<List<MaterialClassificationTreeDto>> GetMaterialClassificationTree(string product, string? sysName)
        {
            var list = await _materialrepository.GetListAsync(m => m.Product == product);
            var materialClassificationtrees = GetEducationChildren(list, 0, sysName);
            return materialClassificationtrees;
        }

        /// <summary>
        /// 查询树形物料产品分类子集
        /// </summary>
        /// <param name="materialClassifications">物料产品分类集合</param>
        /// <param name="pid">物料产品分类父级</param>
        /// <param name="sysName">名称</param>
        /// <returns>返回树形物料产品分类DTO</returns>
        public List<MaterialClassificationTreeDto> GetEducationChildren(List<MaterialClassification> materialClassifications, int pid, string? sysName)
        {
            var materialClassification = materialClassifications.Where(x => x.Pid == pid).ToList();
            List<MaterialClassificationTreeDto> materialClassificationtrees = new List<MaterialClassificationTreeDto>();
            foreach (var item in materialClassification)
            {
                MaterialClassificationTreeDto materialClassificationtree = new MaterialClassificationTreeDto();
                materialClassificationtree.id = item.Id;
                materialClassificationtree.title = item.SysName;
                if (!string.IsNullOrEmpty(sysName))
                {
                    if (materialClassificationtree.title.Contains(sysName!)) materialClassificationtree.spread = true;
                    else materialClassificationtree.spread = false;
                }
                else
                {
                    materialClassificationtree.spread = true;
                }
                materialClassificationtree.children = GetEducationChildren(materialClassifications, item.Id, sysName);
                if (materialClassificationtree.children.Count(x => x.spread == true) > 0) materialClassificationtree.spread = true;
                materialClassificationtrees.Add(materialClassificationtree);
            }
            return materialClassificationtrees;
        }
        /// <summary>
        /// 显示物料产品分类信息
        /// </summary>
        /// <returns>返回结果</returns>
        public List<MaterialClassificationDto> GetMaterialClassification()
        {
            List<MaterialClassificationDto> materialClassificationDtos = _materialrepository.GetListAsync(false).Result.Select(x => new MaterialClassificationDto
            {
                Id = x.Id,
                SysName = x.SysName,
            }).ToList();
            return materialClassificationDtos;
        }


        #region 裴东红
        /// <summary>
        /// 调用递归获取物料产品分类
        /// </summary>
        /// <param name="sysName"></param>
        /// <param name="enAbled"></param>
        /// <returns></returns>
        public List<MaterialTreeDTO> GetMaterialType(string? sysName, int? enAbled)
        {
            var list= GetMaterialTypeInfo(sysName, enAbled, 0).Result;
            return list;
        }

        /// <summary>
        /// 递归
        /// </summary>
        /// <param name="sysName"></param>
        /// <param name="enAbled"></param>
        /// <param name="pid"></param>
        /// <returns></returns>
        public  async Task<List<MaterialTreeDTO>> GetMaterialTypeInfo(string? sysName, int? enAbled, int pid)
        {
            var list = await _materialrepository.GetListAsync(x => ((x.Pid == pid) && (string.IsNullOrEmpty(sysName) || x.SysName.Contains(sysName)) && (enAbled == null || x.EnAbled == enAbled)));

            List<MaterialTreeDTO> resp = new List<MaterialTreeDTO>();

            foreach (var item in list)
            {
                MaterialTreeDTO model = AutoMapperExt.MapTo<MaterialTreeDTO>(item);
                model.Children = GetMaterialTypeInfo(sysName, enAbled, item.Id).Result;
                resp.Add(model);
            }

            return resp;
        }
        //-------------------------------------------------------------------------------
        /// <summary>
        /// 获取产品类型列表
        /// </summary>
        /// <param name="sysName"></param>
        /// <returns></returns>
        public List<MaterialTreeDTO> GetMaterialProductsTypeList(string? sysName)
        {
            var list = _materialrepository.GetListAsync().Result.ToList();

            //var outList = ObjectMapper.Map<List<MaterialProductType>, List<MaterialProductTypeDto>>(list);
            var outList = list.MapToList<MaterialTreeDTO>();
            if (!string.IsNullOrEmpty(sysName))
            {
                outList = outList.Where(x => x.SysName.Contains(sysName)).ToList();
            }
            return outList;
        }

        #endregion
    }
}
